//
//  RYTicketChoiceView.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/3/18.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit

class DYTicketChoiceView: SYBaseView  , UITableViewDelegate, UITableViewDataSource {
    
    var modelClosure:(([DYJQTicketChoiceModel])->Void)?
    var idsClosure: (([String], [String])->Void)?
    var tableView: UITableView!
    var jqModels:[DYJQTicketChoiceModel] = []
    var ids:[String] = []
    var titles: [String] = []
    
    var bgView: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 10.autoWidth()
        v.layer.masksToBounds = true
        return v
    }()
    
    var bgBt: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()
    
    var compBt: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = Constant.blueColor
        v.layer.cornerRadius = 10.autoWidth()
        v.layer.masksToBounds = true
        v.setTitle("完成", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 18.autoBoldFontSize()
        return v
    }()
    
    
 
    
    func configure(_ models: [DYJQTicketChoiceModel]) {
        self.jqModels = models
        self.modelClosure?(models)
        let ms = models.filter { $0.dictionaryName == "成人票" }
        self.ids.append(ms.first?.id ?? "")
        self.titles.append(ms.first?.dictionaryName ?? "")
    }
    
  
    func show() {
        self.isHidden = false
        
        tableView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(45*jqModels.count.autoWidth())
        }
       
        tableView.reloadData()
    }
    
    override func initViews() {
        self.isHidden = true
        bgBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        compBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        
        self.backgroundColor = .clear
        addSubview(bgBt)
        addSubview(bgView)
        addSubview(compBt)
        setTableView()
        
        bgView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(285.autoWidth() + BOTTOM_SAFE_HEIGHT)
        }
        bgBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        compBt.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10.autoWidth() - BOTTOM_SAFE_HEIGHT)
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(44.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(180.autoWidth())
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect.zero, style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(DYTicketChoiceCell.self, forCellReuseIdentifier: "Cell")
        self.bgView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return jqModels.count
    
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: DYTicketChoiceCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DYTicketChoiceCell
        cell?.selectionStyle = .none
        cell?.currentVC = self.currentVC
        cell?.configureView(jqModels[indexPath.row])
        cell?.clickedClosure = {[weak self] value in
            if let `self` = self {
                if value == 1 {
                    self.ids.append(self.jqModels[indexPath.row].id ?? "")
                    self.titles.append(self.jqModels[indexPath.row].dictionaryName ?? "")
                    self.idsClosure?(self.ids, self.titles)
                } else {
                    self.ids = self.ids.filter { $0 != (self.jqModels[indexPath.row].id ?? "") }
                    self.titles = self.titles.filter { $0 != (self.jqModels[indexPath.row].dictionaryName ?? "") }
                    self.idsClosure?(self.ids, self.titles)
                }
            }
        }
        return cell!
    }
    
}



class DYTicketChoiceCell: SYBaseCell {
    
    var count: Int = 0
    let jianPic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.sy_name("ticket_choice")
        v.isUserInteractionEnabled = true
        return v
    }()
    
    var jianBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        
        return v
    }()
    
    let countLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    
    public func configureView(_ model: DYJQTicketChoiceModel) {
        if model.dictionaryName == "成人票" {
            jianAction()
        }
        nameLb.text = model.dictionaryName
    }
    
    override func initViews() {
        
        jianBt.addTarget(self, action: #selector(jianAction), for: .touchUpInside)
        
        contentView.addSubview(jianPic)
        contentView.addSubview(jianBt)
        
        contentView.addSubview(countLb)
        contentView.addSubview(nameLb)
        contentView.addSubview(lineView)
        
        jianPic.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(32.autoWidth())
            make.width.equalTo(32.autoWidth())
        }
       
        jianBt.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-10.0))
            make.height.equalTo(40.autoWidth())
            make.width.equalTo(40.autoWidth())
        }
     
        countLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-31.0))
            make.height.equalTo(16.autoWidth())
            make.width.equalTo(38.autoWidth())
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(0.5.autoWidth())
        }
        
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.height.equalTo(15.autoWidth())
        }
    }
    
    @objc func jianAction() {
        if count % 2 == 0 {
            jianPic.sy_name("ticket_choice_select")
            clickedClosure?(1)
        } else {
            jianPic.sy_name("ticket_choice")
            clickedClosure?(0)
        }
        count += 1
    }

}
